<?php
/**
 * @package pmapi
 * @copyright Copyright (c)2014 Enrico Fagnoni
 * @license GNU General Public License version 3, or later
 *
 * @since 1.0.0
 */
 
defined('_JEXEC') or die;

define ('EPSILON', 0.0000 );  // just for float arithmetics

class ApiHelper 
{
    const ANONYMOUS_SHOPPERGROUP_ID =2;
    const EMPTY_DB_DATE = '0000-00-00 00:00:00';
    
    public static function iso2Unifact($um)
    {
        // units used by virtuemart vs units used by goodrelations
        static $ISO2UNIFACT = array(
            'M' =>'MTR',
            'CM'=>'CMT',
            'MM'=>'MMT',
            'YD'=>'YRD',
            'FT'=>'FOT',
            'IN'=>'INH',
            'KG'=>'KGM',
            'G' =>'GRM',
            'MG'=>'MGM',
            'LB'=>'LBR',
            'OZ'=>'ONZ',
        );
        return isset($ISO2UNIFACT[$um])?$ISO2UNIFACT[$um]:$um;
    }
    
    
    public static function getLDPType()
    {
         return (is_null(JFactory::getApplication()->input->get('id',null)))?'Resource':'DirectContainer';
    } 

    
    public static function getAgentId()
    {
        return JFactory::getApplication()->input->server->get('HTTP_USER_AGENT','unknown');
    }    
     
      
    public static function setHttpLink( $link, $rel, $appendIt=false )
    {
        $link = htmlspecialchars_decode($link); // http do non need html encoding for uri....
        return JResponse::setHeader('Link',"<$link>; rel=\"$rel\"", $appendIt);
    }

   
    public static function getLang()
    {  
        static $l = 'en'; // set to null when internationalization will ready...
        if (is_null($l)){
            $lang = JFactory::getLanguage();
            $locales = $lang->getLocale();
            $l = $locales[4];
        }
        return $l;
    } 
    
    /*
     * Returns a string literals with language decorator in turtle
     */
    public static function _T($s)
    {  
        return '"""'.JText::_($s).'"""@'. self::getLang();
    }
    
    
    public static function datetime($date='now')
    {
        return date('c',strtotime($date));
    }
 
    
    public static function debugInfo($var,$justPublic=true)
    {
        if ($var instanceof JObject) {
            $result = $var->getProperties($justPublic);
        } elseif (is_object($var)) {
            // simulate JOBject
            $vars = get_object_vars($var);
            if( $justPublic) foreach ($vars as $key => $value)if ('_' == substr($key, 0, 1))unset($vars[$key]);
            
            $result = $vars;  
                      
        } else {
            $result = $var;
        }
        
        return print_r($result,true);
    }


    public static function filterAndRebaseImages($type, array $images)
    {
        $result = array();
        foreach ($images as $image){
            if( $image instanceof VmMediaHandler
                    && $image->published 
                    && $image->file_url
                    && $image->media_role=='file_is_displayable' 
                    && !$image->file_is_forSale
                    && $image->file_type==$type){
                $cleanImage = clone($image);
                // strange... it seams that virtuemart accepts uri with spaces inside. Anyway...
                $cleanImage->file_url = JURI::base(true).'/'.str_replace(' ', '%20', $image->file_url);
                $cleanImage->file_url_thumb = $image->file_url_thumb
                    ?JURI::base(true).'/'.str_replace(' ', '%20', $image->file_url_thumb)
                    :'';
                $result[] = $cleanImage;
            }
        }
        
        return $result;
    } 

}